package com.gzh.core.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Slf4j
public class StringUtils {

    /**
     * UUID
     *
     * @return
     */
    public static String getUUID() {
        return UUID.randomUUID().toString().replace("-", "").toLowerCase();
    }

    /**
     * 日期
     *
     * @return
     */
    public static String formatDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return sdf.format(new Date());
    }

    public static String formatTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");
        return sdf.format(new Date());
    }

    /**
     * 六位验证码
     */
    public static String getVerificationCode() {
        //  获取6为随机验证码
        String[] letters = new String[]{
                "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "a", "s", "d", "f", "g", "h", "j", "k", "l", "z", "x", "c", "v", "b", "n", "m",
                "A", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "A", "S", "D", "F", "G", "H", "J", "K", "L", "Z", "X", "C", "V", "B", "N", "M",
                "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
        String stringBuilder = "";
        for (int i = 0; i < 6; i++) {
            stringBuilder = stringBuilder + letters[(int) Math.floor(Math.random() * letters.length)];
        }
        return stringBuilder;
    }

    /**
     * 六位验证码(纯数字)
     */
    public static String getVerificationNumberCode() {
        //  获取6为随机验证码
        String[] letters = new String[]{
                "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
        String stringBuilder = "";
        for (int i = 0; i < 6; i++) {
            stringBuilder = stringBuilder + letters[(int) Math.floor(Math.random() * letters.length)];
        }
        return stringBuilder;
    }

    public static boolean isEmpty(String str) {
        return org.springframework.util.StringUtils.isEmpty(str);
    }

    public static boolean isNotEmpty(String str) {
        return org.springframework.util.StringUtils.hasText(str);
    }

    public static String toString(Object obj) {
        return obj != null ? String.valueOf(obj) : null;
    }

    public static String toString(Object obj, String defaultStr) {
        return obj != null ? String.valueOf(obj) : defaultStr;
    }

    public static String toString(Double obj) {
        if (obj == null) {
            return null;
        }
        NumberFormat nf = NumberFormat.getInstance();
        //不使用分组（使用分组则整数位会逗号分隔）
        nf.setGroupingUsed(false);
        //设置最大保留12位小数
        nf.setMaximumFractionDigits(12);
        return nf.format(obj);
    }

    public static String toString(Double obj, String defaultStr) {
        if (obj == null) {
            return defaultStr;
        }
        NumberFormat nf = NumberFormat.getInstance();
        //不使用分组（使用分组则整数位会逗号分隔）
        nf.setGroupingUsed(false);
        //设置最大保留12位小数
        nf.setMaximumFractionDigits(12);
        return nf.format(obj);
    }

    public static String trimNull(String obj) {
        return obj == null ? "" : obj;
    }

    public static String listToString(List<String> list) {
        if (CollectionUtils.isEmpty(list)) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        for (String s : list) {
            sb.append(s + ",");
        }
        return sb.toString().substring(0, sb.length() - 1);
    }


    public static String formatTime(Integer date, Integer time) {
        String dateString = toString(date);
        String timeString = toString(time);
        String mouth = dateString.substring(4, 6);
        String day = dateString.substring(6, 8);
        String hour = timeString.substring(0, 2);
        String minte = timeString.substring(2, 4);
        return mouth + "月" + day + "日" + hour + ":" + minte;
    }

    public static Integer getDifferDays(Integer startDate, Integer endDate) throws ParseException {
        if (startDate > endDate){
            return -1;
        }
        DateFormat dft = new SimpleDateFormat("yyyyMMdd");
        Date star = dft.parse(toString(startDate));//开始时间
        Date endDay = dft.parse(toString(endDate));//结束时间
        Date nextDay = star;
        int i = 0;
        while (nextDay.before(endDay)) {//当明天不在结束时间之前是终止循环
            Calendar cld = Calendar.getInstance();
            cld.setTime(star);
            cld.add(Calendar.DATE, 1);
            star = cld.getTime();
            //获得下一天日期字符串
            nextDay = star;
            i++;
        }
        return i;
    }

    public static Integer addTime(Integer time,Integer second){
        SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");
        Date parse = null;
        try {
            parse = sdf.parse(toString(time));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date afterDate = new Date(parse.getTime() + 60000 * second);
        return NumericHelper.toInteger(sdf.format(afterDate));
    }
}
